<?php
/**
 * User: yuesir
* Date: 2016/4/27
* Time: 23:15
 * 数据对象映射模式，将对字段的操作转化成对属性的操作
 * 
*/
namespace Lib;

class User
{
    public $id;
    public $name;
    public $mobile;
    public $regtime;

    protected $db;

    function __construct($id)
    {
        $this->db = new \Lib\Database\MySQLi();
        $this->db->connect('localhost', 'root', '', 'test');
        $res = $this->db->query("select * from user where id={$id} limit 1");
        $data = $res->fetch_assoc();

        $this->id = $data['id'];
        $this->name = $data['name'];
        $this->mobile = $data['mobile'];
        $this->regtime = $data['regtime'];

        return $data;
    }

    function __destruct()
    {
        $this->db->query("update user set name='{$this->name}', mobile='{$this->mobile}', regtime='{$this->regtime}'
                          where id={$this->id} limit 1");

    }
}